﻿#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.IO;
using System.Text;
using NUnit.Framework;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web;
using Moq;
using Newtonsoft.Json.Converters;

namespace Newtonsoft.Json.Web.Tests
{
  public class JsonNetResultTests : TestFixtureBase
  {
    [Test]
    public void ExecuteResult_BasicString()
    {
      StringWriter output = new StringWriter();
      IMock<HttpContextBase> context = new Mock<HttpContextBase>();
      context.Expect(c => c.Response.Output).Returns(output);

      string value = string.Empty;
      context.ExpectGet(c => c.Response.ContentType).Returns(() => value);
      context.ExpectSet(c => c.Response.ContentType).Callback(p => value = p);

      IMock<ControllerContext> controllerContext = new Mock<ControllerContext>(
        context.Object,
        new Mock<RouteData>().Object,
        new Mock<ControllerBase>().Object);

      JsonNetResult result = new JsonNetResult();
      result.Data = "Test";

      result.ExecuteResult(controllerContext.Object);

      Assert.AreEqual(@"""Test""", output.ToString());
      Assert.AreEqual(@"application/json", controllerContext.Object.HttpContext.Response.ContentType);
    }

    [Test]
    public void ExecuteResult_ShouldUseCustomContentTypeAndEncoding()
    {
      StringWriter output = new StringWriter();
      IMock<HttpContextBase> context = new Mock<HttpContextBase>();
      context.Expect(c => c.Response.Output).Returns(output);

      string contentType = string.Empty;
      context.ExpectGet(c => c.Response.ContentType).Returns(() => contentType);
      context.ExpectSet(c => c.Response.ContentType).Callback(p => contentType = p);

      Encoding encoding = null;
      context.ExpectGet(c => c.Response.ContentEncoding).Returns(() => encoding);
      context.ExpectSet(c => c.Response.ContentEncoding).Callback(p => encoding = p);

      IMock<ControllerContext> controllerContext = new Mock<ControllerContext>(
        context.Object,
        new Mock<RouteData>().Object,
        new Mock<ControllerBase>().Object);

      JsonNetResult result = new JsonNetResult();
      result.Data = 1;
      result.ContentType = "application/text";
      result.ContentEncoding = Encoding.ASCII;

      result.ExecuteResult(controllerContext.Object);

      Assert.AreEqual("1", output.ToString());
      Assert.AreEqual(@"application/text", controllerContext.Object.HttpContext.Response.ContentType);
      Assert.AreEqual(encoding, controllerContext.Object.HttpContext.Response.ContentEncoding);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: context")]
    public void ExecuteResult_ShouldThrowOnNullContext()
    {
      JsonNetResult result = new JsonNetResult();
      result.ExecuteResult(null);
    }

    [Test]
    public void ExecuteResult_NoData()
    {
      StringWriter output = new StringWriter();
      IMock<HttpContextBase> context = new Mock<HttpContextBase>();
      context.Expect(c => c.Response.Output).Returns(output);

      string value = string.Empty;
      context.ExpectGet(c => c.Response.ContentType).Returns(() => value);
      context.ExpectSet(c => c.Response.ContentType).Callback(p => value = p);

      IMock<ControllerContext> controllerContext = new Mock<ControllerContext>(
        context.Object,
        new Mock<RouteData>().Object,
        new Mock<ControllerBase>().Object);

      JsonNetResult result = new JsonNetResult();

      result.ExecuteResult(controllerContext.Object);

      Assert.AreEqual(string.Empty, output.ToString());
      Assert.AreEqual(@"application/json", controllerContext.Object.HttpContext.Response.ContentType);
    }

    [Test]
    public void ExecuteResult_ShouldUseFormat()
    {
      StringWriter output = new StringWriter();
      IMock<HttpContextBase> context = new Mock<HttpContextBase>();
      context.Expect(c => c.Response.Output).Returns(output);

      IMock<ControllerContext> controllerContext = new Mock<ControllerContext>(
        context.Object,
        new Mock<RouteData>().Object,
        new Mock<ControllerBase>().Object);

      JsonNetResult result = new JsonNetResult();
      result.Formatting = Formatting.Indented;
      result.Data = new object[] { 1, 2, new DateTimeOffset(2000, 11, 11, 11, 11, 11, TimeSpan.Zero) };

      result.ExecuteResult(controllerContext.Object);

      Assert.AreEqual(@"[
  1,
  2,
  ""\/Date(973941071000+0000)\/""
]", output.ToString());
    }

    [Test]
    public void ExecuteResult_ShouldUseSerializationSettings()
    {
      StringWriter output = new StringWriter();
      IMock<HttpContextBase> context = new Mock<HttpContextBase>();
      context.Expect(c => c.Response.Output).Returns(output);

      IMock<ControllerContext> controllerContext = new Mock<ControllerContext>(
        context.Object,
        new Mock<RouteData>().Object,
        new Mock<ControllerBase>().Object);

      JsonNetResult result = new JsonNetResult();
      result.SerializerSettings.Converters.Add(new JavaScriptDateTimeConverter());
      result.Data = new object[] { 1, 2, new DateTimeOffset(2000, 11, 11, 11, 11, 11, TimeSpan.Zero) };

      result.ExecuteResult(controllerContext.Object);

      Assert.AreEqual(@"[1,2,new Date(973941071000)]", output.ToString());
    }
  }
}